const app = getApp()
Page({
  data: {
    telphone:'',
    item:{},
    coupon:{}
  },
  //监听页面初次载入完成
  onLoad: function (options) {
    this.data.id = options.id
  },
  //监听页面初次载入完成
  onShow: function () {
    app.isLogin((rel)=>{
      this.getApi();
    })
  },
  //读取我的订单
  getApi: function() {
    var param = {
      signkey: app.util().getRandom(12),
      id: this.data.id
    }
    app.api().Get('api/v4/allwin/shopdopay/cart',param,(result) => {
      if(result.code == 200){
        this.setData({
          item: result.data,
          telphone:result.data.telphone
        });
      }
    })
  },
  //提交订单
  onSubmit: function(){
    var param = {
      telphone:this.data.telphone,
      message:this.data.message,
      shop_id:this.data.id,
      ucode: app.globalData.ucode,
      signkey: app.util().getRandom(12),
    }
    app.api().Post('api/v4/allwin/shopdopay/index',param,(res) => {
      if (res.code == 200) {
       app.doWechatPay(res.data,()=>{
          wx.redirectTo({
            url: '../order/index?active=2',
          })
       },()=>{
        wx.showModal({
          title:'微信支付',content:'你已取消支付,是否重试',
          cancelText:'取消',confirmText	:'重试',
          success:(res) => {
            if (res.confirm) {
              this.onSubmit();
            }
          }
        })
       })
      }
    })
  },
  //保存表单值
  telphone: function(e) {
    this.data.telphone = e.detail;
  },
  message: function(e) {
    this.data.message = e.detail;
  }
})